package com.ynet.finmall.core.config;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class FMPropertiesSetting {

	/**
	 * 运行时值注入 
	 * Environment
	 */
	@Bean
	public PropertySourcesPlaceholderConfigurer placeholderConfigurer(){

		Properties[] propertiesArray = new Properties[2];
		propertiesArray[0] = new Properties();
		propertiesArray[1] = new Properties();
		
		try {
			propertiesArray[0].load(new ClassPathResource("application.properties").getInputStream());
			String settingPrefix = propertiesArray[0].getProperty("spring.profiles.active");
			if(!StringUtils.isBlank(settingPrefix)){
				propertiesArray[1].load(new ClassPathResource("application-" + settingPrefix + ".properties").getInputStream());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
		placeholderConfigurer.setPropertiesArray(propertiesArray);
		
		return placeholderConfigurer;
	}
	
}
